import pymysql.cursors

conn = pymysql.connect(host='localhost', user='root', password='', db='test', port=3306, charset='utf8',
                       cursorclass=pymysql.cursors.DictCursor)


def query(sql, rows="all"):
    result = []
    with conn.cursor() as cursor:
        cursor.execute(sql)
        if rows == "all":
            result = cursor.fetchall()
        else:
            result = cursor.fetchmany(rows)
        return result

def commit():
    conn.commit()

def insert(sql):
    with conn.cursor() as cursor:
        cursor.execute(sql)
        return cursor.lastrowid
